﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NJL.WebUsers.AdminVisitor
{
    public partial class Default : BasePage
    {
        NJL.BLL.NJL_NEWS bllNews = new BLL.NJL_NEWS();
        NJL.BLL.NJL_MYFRIEND bllFriend = new BLL.NJL_MYFRIEND();
        NJL.BLL.NJL_MY_COLLECTION bllCollection = new BLL.NJL_MY_COLLECTION();
        NJL.BLL.NJL_PERSON bllPerson = new BLL.NJL_PERSON();
        Model.NJL_PERSON modelPerson;
        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();
        NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        protected NJL.Model.NJL_COMPANY modelCompany;
        protected void Page_Load(object sender, EventArgs e)
        {
            modelCompany = bllCompany.GetCompanyByUserIDModel(UserId);
            if (modelCompany != null)
            {

            }
            if (!IsPostBack)
            {
                Page.Title = UserName + "的用户中心";
                bind();
            }

        }
        void bind()
        {
            DataSet ds = bllNews.GetList("NJL_PERSON_ID=" + UserId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                lityj.Text = ds.Tables[0].Rows.Count.ToString();
            }
            else
            {
                lityj.Text = "0";
            }
            ds = bllFriend.GetList("NJL_MYFRIEND.NJL_USER_ID=" + UserId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                lithy.Text = ds.Tables[0].Rows.Count.ToString();
            }
            else
            {
                lithy.Text = "0";
            }
            NJL.Model.NJL_USER modelUser = bllUser.GetModel(UserId);
            if (modelUser.INTEGRAL != null)
            {
                litIntegral.Text = modelUser.INTEGRAL.ToString();
            }
            else
            {
                litIntegral.Text = "0";
            }
            //DataSet dsCollection = bllCollection.GetList("NJL_USER_ID="+UserId);
            //if (dsCollection.Tables[0].Rows.Count > 0)
            //{
            //    litsc.Text = ds.Tables[0].Rows.Count.ToString();
            //}
            //else
            //{
            //    litsc.Text = "0";
            //}
            modelPerson = bllPerson.GetModelByUserId(UserId);
            if (modelPerson.IMAGE_CODE != "")
            {
                imghead.ImageUrl = GetImageUrl(modelPerson.IMAGE_CODE);
            }
            else
            {
                imghead.ImageUrl = "~/Images/avatar.jpg";
            }

            if (modelPerson.EVALUATION_UPDATE_DATE_TIME.ToString() == "")
            {
                pUpdateDatetime.InnerHtml = "好懒哦,写点东西嘛...";
                pUpdateState.InnerHtml = "";
            }
            else
            {
                pUpdateDatetime.InnerHtml = "上次更新是：" + modelPerson.EVALUATION_UPDATE_DATE_TIME.Value.ToString("yyyy-MM-dd HH:mm:ss");
                pUpdateState.InnerHtml = Server.HtmlEncode(modelPerson.SELF_EVALUATION);
            }
            #region 农家乐推荐
            NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
            if (WEBSITE_FOCUS_CITY_CODE != "")
            {

                ds = bllCompany.GetList(9, "NJL_COMPANY.TYPE_CODE='1' and NJL_COMPANY.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' and NJL_COMPANY.AUDIT_STATE=1 and NJL_COMPANY.AREA_CODE='" + WEBSITE_FOCUS_CITY_CODE + "' and NJL_COMPANY.IMAGE_CODE is not null", "CREATE_DATE_TIME desc");
            }
            else
            {
                ds = bllCompany.GetList(9, "NJL_COMPANY.TYPE_CODE='1' and NJL_COMPANY.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' and NJL_COMPANY.AUDIT_STATE=1 and NJL_COMPANY.IMAGE_CODE is not null", "CREATE_DATE_TIME desc");
            }
            rptNjl.DataSource = ds;
            rptNjl.DataBind();
            #endregion
            #region 游客分享
            ds = bllNews.GetList(9, "NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'", "VISIT_COUNT desc");
            rptShare.DataSource = ds;
            rptShare.DataBind();
            #endregion
            #region 推荐服务
            NJL.BLL.NJL_COMPANY_SERVICE bllService = new BLL.NJL_COMPANY_SERVICE();
            if (WEBSITE_FOCUS_CITY_CODE != "")
            {
                ds = bllService.GetList(5, "NJL_COMPANY_ID in(select ID from NJL_COMPANY where NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' and AUDIT_STATE=1 and AREA_CODE='" + WEBSITE_FOCUS_CITY_CODE + "') and IMAGE_CODE is not null", "VISIT_COUNT desc");
            }
            else
            {
                ds = bllService.GetList(5, "NJL_COMPANY_ID in(select ID from NJL_COMPANY where NJL_SYS_BASE_ID='" + WEBSITE_CODE + "') and AUDIT_STATE=1 and IMAGE_CODE is not null", "VISIT_COUNT desc");
            }
            rptService.DataSource = ds;
            rptService.DataBind();
            #endregion

        }
        protected void ibtnSubmit_Click(object sender, ImageClickEventArgs e)
        {
            string strMyState = this.txtMyState.Text.Trim();
            Model.NJL_PERSON model = bllPerson.GetModelByUserId(UserId);
            model.SELF_EVALUATION = strMyState;
            model.EVALUATION_UPDATE_DATE_TIME = DateTime.Now;
            if (bllPerson.Update(model))
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "发布成功！", "Default.aspx");
            }
            else
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "发布失败！请重试", "Default.aspx");
            }
            bind();
        }
    }
}